<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 数组，基准值，右边哨兵
        function quickSort(arr, from, to) {
            let i = from; // 左哨兵
            let j = to; // 右哨兵
            const key = arr[from];
            if (from >= to) { //如果数组只有一个元素
                return;
            }
            while (i < j) {
                while (arr[j] > key && i < j) {
                    j--;
                }
                while (arr[i] <= key && i < j) {
                    i++;
                }

                if (i < j) {
                    const temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }

            arr[from] = arr[i];
            arr[i] = key;
            quickSort(arr, from, i - 1);
            quickSort(arr, i + 1, to);

        }

        let arr = [3, 3, -5, 6, 0, 2, -1, -1, 3];
        console.log(arr);
        quickSort(arr, 0, arr.length - 1);
        console.log(arr);
    </script>
</body>

</html>